﻿
cloud_gaming_control = 
{
    init = function()
        ierd_tgp.tpf_ui.Require("cloud_gaming\\model.lua");
        ierd_tgp.tpf_ui.Require("cloud_gaming\\cloud_game_protocol.lua");
        comm_center.reg_service("SvrLua_CloudGame_AvailableTimeRemind", 
              cloud_gaming_control.On_CloudGameAavailableTimeRemind);
        comm_center.reg_service("SvrLua_CloudGame_Purchase_Request",
            cloud_gaming_control.On_CloudGame_Purchase_Request);
        comm_center.reg_service("Svr_CloudGamePurchase_Request",
            cloud_gaming_control.On_CloudGame_Purchase_Request);
        comm_center.reg_service("SvrLua_CloudGame_Launch_Request", 
            cloud_gaming_control.on_launch_cloudgame_request);
        cloud_gaming_control.inner_log("init", "init cloud gaming");
    end;

    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[cloud_gaming_control] %s", msg), core_msg);
    end;

    on_login_success = function(event_id, json_str)
        local info = base_utility.json_to_table(json_str);
        if cloud_gaming_model.uin == 0 then
            cloud_gaming_model.uin = info.uin;
        end;
        cloud_gaming_control.check_cloud_gaming_guide_record();
    end;

    check_cloud_gaming_guide_record = function()
        cloud_gaming_control.inner_log("check server");
        base_utility.get_accumulate_count(cloud_gaming_model.server_record_item,
            cloud_gaming_model.server_record_sub_item,
            tostring(cloud_gaming_model.uin), "cloud_gaming_control.on_check_cloud_gaming_guide_record");
    end;

    check_local_cloud_gaming_guide_record = function ()
        local config = cloud_gaming_model.get_local_cloud_gaming_guide_cfg();
        if nil == config or config.exits == nil or  0 == config.exits then
            cloud_gaming_control.inner_log("no local record : ");
            cloud_gaming_model.guide_has_shown = false;
            cloud_gaming_control.check_should_show_guide();
        else 
            cloud_gaming_control.inner_log("has local record : "..tostring(config.value));
        end;
    end;

    on_check_cloud_gaming_guide_record = function(json_str)
        cloud_gaming_control.inner_log("on_check_cloud_gaming_guide_record : "..json_str);
        if json_str == nil then
             cloud_gaming_control.check_local_cloud_gaming_guide_record();
            return;
        end
        local result_tb = base_utility.json_to_table(json_str);
        if result_tb == nil or result_tb.result == nil then
            cloud_gaming_control.check_local_cloud_gaming_guide_record();
            return;
        end

        if 0 == result_tb.result then
            local list = result_tb.sub_item_list;
            local query_sub_item = false;
            if list == nil or type(list) ~= "table" or nil == next(list) then  
                cloud_gaming_model.guide_has_shown = false;
            else
                local item_count = #list;
                cloud_gaming_control.inner_log("on_check_cloud_gaming_guide_record : item_count"
                    ..tostring(item_count));
                for id = 1, item_count do
                    local sub_item_list = list[id];
                    if sub_item_list ~=nil and sub_item_list.sub_item ~= nil and
                       sub_item_list.sub_item == cloud_gaming_model.server_record_sub_item then
                        if sub_item_list.is_reported == nil or sub_item_list.is_reported == 0 then
                            cloud_gaming_model.guide_has_shown = false;
                        else
                            cloud_gaming_model.guide_has_shown = true;
                        end;
                        query_sub_item = true;
                        break;
                    end;
                end;
            end;

            if query_sub_item == false then
                cloud_gaming_model.guide_has_shown = false;
            end;
            cloud_gaming_control.check_should_show_guide();
        end
    end;

    check_should_show_guide = function()
        if base_utility.CLIENT_TYPE.OVERSEA_VER == base_utility.get_client_version_type() then
            return;
        end

        if true == base_utility.is_develop_client() then
            return;
        end

        -- 已经查询到是否需要显示引导
        if cloud_gaming_model.guide_has_shown == nil or 
           cloud_gaming_model.guide_has_shown == true then
            cloud_gaming_control.inner_log("guide has shown, no need show");
            return;
        end

        if cloud_gaming_model.show_entry_game_id == nil or 
           cloud_gaming_model.cur_focus_game_id == nil or
           cloud_gaming_model.show_entry_game_id ~= cloud_gaming_model.cur_focus_game_id then
            cloud_gaming_control.inner_log("cur focus game id not equal cloud gaming id");
            return;
        end

        cloud_gaming_control.inner_log("show_entry_game_id:"
        .. tostring(cloud_gaming_model.show_entry_game_id)
        .. ",cur_focus_game_id:" .. tostring(cloud_gaming_model.cur_focus_game_id));
        wegame_mainframe_view.set_pack_up_btn_visible(false);
        cloud_gaming_control.show_cloud_gaming_guide();
    end;

    handle_banner_switch = function(event_id, json_str)
        if json_str == nil then
            return;
        end
        local params = base_utility.json_to_table(json_str);
        local game_id = nil;
        if params ~= nil then
            if params.module_id ~= nil then
                game_id = tonumber(params.module_id);
            end
        end
      
        if game_id == nil then
            return;
        end
        if cloud_gaming_model.cur_focus_game_id == game_id then
            return;
        end

        cloud_gaming_model.cur_focus_game_id = game_id;
        cloud_gaming_model.show_entry_game_id = nil;
    end;

    on_cloud_game_cfg_info_brc = function(param)
        if param == nil or param.game_id == nil or param.game_id == 0 then
            return;
        end

        if param.no_data == nil or param.no_data == true then
            if param.game_id ~= nil then
                cloud_gaming_control.inner_log("on_cloud_game_cfg_info_brc not cloud game:"
                    ..tostring(param.game_id));
            else
                cloud_gaming_control.inner_log("on_cloud_game_cfg_info_brc not cloud game nil");
            end;
            return;
        end;

        cloud_gaming_model.show_entry_game_id = param.game_id;
        cloud_gaming_control.check_should_show_guide();
    end;

    on_close_main_wnd = function (data)
    end;

    show_cloud_gaming_guide = function ()
        cloud_gaming_control.inner_log("show cloud gaming guide");
        wegame_mainframe_view.set_pack_up_btn_visible(false);
        local params = {
            url = LoadStr("CLOUD_GAMING_GUIDE_URL"),
            report_type = "yunyouxi_guide",
            need_focus_home = false,
            guide_show_callback = function()
                cloud_gaming_model.guide_has_shown = true;
                cloud_gaming_model.set_server_cloud_gaming_guide_cfg();
                return;
            end,
            guide_close_callback = function()
                 wegame_mainframe_view.set_pack_up_btn_visible(true);
                return;
            end,
        };
        simple_user_guide_control.show_custom_user_guide(params);
    end;

    On_CloudGameAavailableTimeRemind = function(data)
        local launch_game_id = data.game_id;
        local zParam = 
        {
            position = "center",
            title = LoadStr("ID_TPF_CLOUD_GAME_TIME_REMIND_TITLE"),
            width = 480,
            height = 230,
            titleIcon = "",
            icon = "",
            tipsIcon = poptips_icons.kInfo,
            descTitle = LoadStr("ID_TPF_CLOUD_GAME_TIME_REMIND_TIPS"),
            desc = LoadStr(""),
            parent_id = main_id,
            modal = true,
            always_center = true,
            close_means_cancel = true,
            okBtn = 
            {
                visible = "true",
                close_win = true,
                delay_close = true,
                text = LoadStr("ID_TPF_CLOUD_GAME_ADD_TIME"),
                handle = function()
                    local purchase_param = {
                       game_id = launch_game_id;
                       parameter = "#/?game_id=" .. tostring(launch_game_id);
                       width = 660;
                       height = 500;
                     };
                    comm_center.call_service("SvrLua_CloudGame_Purchase_Request", purchase_param);
                end
            },
            cancelBtn = 
            {
                visible = "true",
                close_win = true,
                text = LoadStr("ID_TPF_CLOUD_GAME_NOT_ADD_TIME"),
                handle = function()
                     local launch_param = {game_id = launch_game_id,ignore_time = true};
                     comm_center.call_service("SvrLua_CloudGame_Launch_Request", launch_param);
                end
            }
       };

       poptips_control.show_tips(zParam);
    end;
    
    cloudGame_purchase_report = function(game_id)
        local report_data = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local kv_data = {};
        if nil ~= report_data then
            kv_data["game_id"] = game_id;
            report_data.oc_type = 1;
            report_data.qos_kind = ierd_tgp.adapt_for_imports.qos.Qos_kind.kCloudGameOperationReport;
            report_data.str_action = "cloud_game"
            report_data.str_type = "click_add_cloud_game_time";
            report_data.str_json = base_utility.table_to_json(kv_data);
            report_data.game_id = game_id;
            base_utility.lua_qos_ex(report_data);
            report_data:delete();
        end
    end;

    On_CloudGame_Purchase_Request = function(data)
        if data.game_id ~= nil then
            cloud_gaming_control.cloudGame_purchase_report(data.game_id);
        end
        local purchase_url = LoadStr("GL_CLOUD_GAME_PURCHASE_URL");
        local param = {
                    url = purchase_url ..data.parameter;
                    use_ptlogin = true;
                    width = data.width;
                    height = data.height;
                    caption = LoadStr("ID_TPF_CLOUD_GAME_PURCHASE_TIME");
                    show_loading_page = true;
                    has_border = true;
                };
        comm_center.call_service("i_service_show_simple_web_window", param);
    end;

    on_launch_cloudgame_request = function (data)
        local game_id = 0;

        if data.game_id ~= nil then
            game_id = tonumber(data.game_id);
        end

        if game_id == nil or game_id ==0 then
             base_utility.log("cloudgaming on_launch_cloudgame_request gameid 0" );
            return;
        end
        base_utility.log("cloudgaming on_launch_cloudgame_request" .. tostring(game_id), true);
         --  小于5分钟要提示
        if data.ignore_time == nil or data.ignore_time == false then
            local query_game_launch_param = { game_id = game_id};
            local query_ret = comm_center.call_service("Svr_GameLibrary_QueryGameLaunchInfo",
                            query_game_launch_param);
            if query_ret == nil or query_ret.game_info_list == nil then
                 base_utility.log("[cloudgaming ]on_launch_cloudgame_request"
                   .. "error! no game info");
                 return;
            end;
            local game_info = base_utility.json_to_table(query_ret.game_info_list);
            if game_info.available_time < 300 and game_info.available_time >= 0 then
                local remind_param = {game_id = game_id};
                comm_center.call_service("SvrLua_CloudGame_AvailableTimeRemind", remind_param);
                return;
            end;
        end;

        if false == cloud_game_protocol_control:CheckCloudGameProtocol(game_id) then
            return;
        end;
        -- 启动云游戏
        --先判断选区
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local support_zone_select = game_lib:is_zone_select_support(game_id);
        local server_id = 0;
        if support_zone_select then
            base_utility.log("get selected zone");
            local game_servers = banner_model.get_game_servers(game_id);
            if game_servers ~= nil and game_servers.selleted ~= nil then
                server_id = game_servers.selleted;
            end
            if server_id == 0 then
                -- 打开选区面板
                base_utility.log("server_id is nil, open dir_ui");
                comm_center.call_service('Svr_ShowZoneChose', 
                    {game_id = game_id, uin=login_model.main_uin});
                return;
            end;
        end;
        local provider_infos = 
        comm_center.call_service("Svr_TGPConfigMgr_QueryCloudGameProviderInfo", {game_id = game_id});
        if provider_infos == nil or provider_infos.provider_info_list ==nil then
            --  show error
        end;
        local provider_info_list =
        base_utility.json_to_table(provider_infos.provider_info_list);
        if provider_info_list.provider_count == 0 or
        provider_info_list.first_provider_id == nil then
            --  show error
        end;
            
        local first_provider_id = provider_info_list.first_provider_id;
        base_utility.log("simple panel launch cloud game provider_infos.provider_count:"
        .. tostring(provider_infos.provider_count));
        if provider_info_list.provider_count == 1 then
                base_utility.log("provider_infos.provider_count == 1 launch cloud game");
            comm_center.call_service('Svr_LaunchCloudGame', 
                {cloud_channel_id = first_provider_id, game_id = game_id, zone_id = server_id});
            cloud_gaming_control.cloud_game_btn_report(game_id, first_provider_id, server_id);
        else
            if data.channel_id ~= nil then
                comm_center.call_service('Svr_LaunchCloudGame', 
                {cloud_channel_id = data.channel_id, game_id = game_id, zone_id = server_id});
                cloud_gaming_control.cloud_game_btn_report(game_id, data.channel_id, server_id);
            else
                comm_center.call_service('Svr_CloudGame_ShowProviderInfos', {game_id = game_id,});
            end;
        end
    end;

    cloud_game_btn_report = function(game_id, channel_id, zone_id)
        local report_data = ierd_tgp.tpf_ui.LuaQosDataEx:new();
        local kv_data = {};
        if nil ~= report_data then
            kv_data["game_id"] = game_id;
            kv_data["plugin_id"] = channel_id;
            kv_data["zone_id"] = zone_id;
            report_data.oc_type = 1;
            report_data.qos_kind = ierd_tgp.adapt_for_imports.qos.Qos_kind.kCloudGameOperationReport;
            report_data.str_action = "cloud_game"
            report_data.str_type = "click_launch_btn";
            report_data.str_json = base_utility.table_to_json(kv_data);
            report_data.game_id = game_id;
            base_utility.lua_qos_ex(report_data);
            report_data:delete();
        end
    end;
};

cloud_gaming_control.init();
